package com.zkny.zknyerp.config;


import com.alibaba.fastjson.JSONObject;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.Result;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

public class ShiroLoginFilter extends FormAuthenticationFilter {

    /*重写父类方法，过滤option请求*/
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if(request instanceof HttpServletRequest){
            if(((HttpServletRequest) request).getMethod().toUpperCase().equals("OPTIONS")){
                return true;
            }
        }
        return super.isAccessAllowed(request, response, mappedValue);
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.write(JSONObject.toJSONString(new Result(Constant.code_1013, Constant.msg_1013)));
        return false;
    }
}
